package vues.album;

import images.ImageResize;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;

import java.awt.Cursor;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Toolkit;
import javax.swing.SwingConstants;

import vues.album.Recherche;
import vues.RendererNumero;
import java.awt.Color;

public class Album extends JFrame implements ActionListener, MouseListener{
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTable albumTable;
	private JScrollPane scrollAlbumTable;
	private modeles.Albums albums = new modeles.Albums();
	private JLabel lblTitre;
	private JLabel lblGenre;
	private JLabel lblAnnee;
	private JLabel lblNumero;
	private JLabel lblPrix;
	private JLabel lblMaison;
	private JLabel lblArtiste;
	private JButton btnAjouter;
	private JButton btnModifier;
	private JButton btnSupprimer;
	private JButton btnRecherche;
	private JButton btnRetour;
	private JLabel lblTitreAlbum;
	private JLabel lblGenreAlbum;
	private JLabel lblAnneeAlbum;
	private JLabel lblMaisonAlbum;
	private JLabel lblPrixAlbum;
	private JLabel lblNumeroAlbum;
	private JLabel lblArtisteAlbum;
	private JLabel lblPhotoAlbum;


	/**
	 * Create the frame.
	 */
	public Album() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Album.class.getResource("/images/Album.png")));
		setTitle("Menu - Albums");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 689, 404);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblPhotoAlbum());
		contentPane.add(getScrollAlbumTable());
		contentPane.add(getBtnRetour());
		contentPane.add(getLblTitre());
		contentPane.add(getLblGenre());
		contentPane.add(getLblAnnee());
		contentPane.add(getLblNumero());
		contentPane.add(getLblPrix());
		contentPane.add(getLblMaison());
		contentPane.add(getBtnAjouter());
		contentPane.add(getBtnModifier());
		contentPane.add(getBtnSupprimer());
		contentPane.add(getBtnRecherche());
		contentPane.add(getLblTitreAlbum());
		contentPane.add(getLblGenreAlbum());
		contentPane.add(getLblAnneeAlbum());
		contentPane.add(getLblMaisonAlbum());
		contentPane.add(getLblPrixAlbum());
		contentPane.add(getLblNumeroAlbum());
		contentPane.add(getLblArtiste());
		contentPane.add(getLblArtisteAlbum());
		getRootPane().setDefaultButton(btnRetour);
	}

	private JLabel getLblPhotoAlbum() {
		if (lblPhotoAlbum == null) {
			lblPhotoAlbum = new JLabel("");
			lblPhotoAlbum.setHorizontalAlignment(SwingConstants.CENTER);
			lblPhotoAlbum.setBounds(23, 211, 244, 155);
			lblPhotoAlbum.setIcon(ImageResize.createImage(new ImageIcon(Album.class.getResource("/images/Album.png")), lblPhotoAlbum.getWidth(), lblPhotoAlbum.getHeight()));
		}
		return lblPhotoAlbum;
	}

	private JTable getAlbumTable() {
		if (albumTable == null) {
			albums.update();
			albumTable = new JTable(albums);
			albumTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			albumTable.getColumnModel().getColumn(0).setCellRenderer(new RendererNumero());
			albumTable.addMouseListener(this);
			albumTable.setCursor(new Cursor(Cursor.HAND_CURSOR));
			albumTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
				@Override
				public void valueChanged(ListSelectionEvent e) {
					if(e.getSource() == albumTable.getSelectionModel() && e.getValueIsAdjusting()){
						update();
					}
				}				
			});
		}
		return albumTable;
	}
	private JScrollPane getScrollAlbumTable() {
		if (scrollAlbumTable == null) {
			scrollAlbumTable = new JScrollPane(getAlbumTable());
			scrollAlbumTable.setBounds(170, 11, 503, 193);
		}
		return scrollAlbumTable;
	}

	private JButton getBtnRetour() {
		if (btnRetour == null) {
			btnRetour = new JButton("Retour");
			btnRetour.setIcon(new ImageIcon(Album.class.getResource("/images/Boutons/Retour.png")));
			btnRetour.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnRetour.addActionListener(this);
			btnRetour.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnRetour.setBounds(570, 343, 103, 23);
		}
		return btnRetour;
	}
	public void update(){
		albums.update();
		albumTable.revalidate();
		albumTable.repaint();
		if(albumTable.getSelectedRow() >= 0){
			modeles.Album album = albums.getAlbum(albumTable.getSelectedRow());
			lblTitreAlbum.setText(album.getTitre());
			lblGenreAlbum.setText(album.getGenre());
			lblAnneeAlbum.setText("" + album.getAnnee());
			lblNumeroAlbum.setText("" + album.getId());
			lblPrixAlbum.setText("" + album.getPrix());
			lblMaisonAlbum.setText(album.getMaison());
			lblArtisteAlbum.setText(album.getArtiste().getNom());
			btnModifier.setEnabled(true);
			btnSupprimer.setEnabled(true);
			lblPhotoAlbum.setIcon(ImageResize.createImage(album.getCouverture(), lblPhotoAlbum.getWidth(), lblPhotoAlbum.getHeight()));
		}else{
			lblTitreAlbum.setText("");
			lblGenreAlbum.setText("");
			lblAnneeAlbum.setText("");
			lblPrixAlbum.setText("");
			lblMaisonAlbum.setText("");
			lblNumeroAlbum.setText("");
			lblArtisteAlbum.setText("");
			btnModifier.setEnabled(false);
			btnSupprimer.setEnabled(false);
			lblPhotoAlbum.setIcon(ImageResize.createImage(new ImageIcon(Album.class.getResource("/images/Album.png")), lblPhotoAlbum.getWidth(), lblPhotoAlbum.getHeight()));
		}
	}
	private JLabel getLblTitre() {
		if (lblTitre == null) {
			lblTitre = new JLabel("Titre : ");
			lblTitre.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblTitre.setBounds(277, 224, 68, 14);
		}
		return lblTitre;
	}
	private JLabel getLblGenre() {
		if (lblGenre == null) {
			lblGenre = new JLabel("Genre : ");
			lblGenre.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblGenre.setBounds(277, 249, 68, 14);
		}
		return lblGenre;
	}
	private JLabel getLblAnnee() {
		if (lblAnnee == null) {
			lblAnnee = new JLabel("Ann\u00E9e de sortie : ");
			lblAnnee.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblAnnee.setBounds(277, 274, 134, 14);
		}
		return lblAnnee;
	}
	private JLabel getLblNumero() {
		if (lblNumero == null) {
			lblNumero = new JLabel("Num\u00E9ro : ");
			lblNumero.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblNumero.setBounds(520, 224, 79, 14);
		}
		return lblNumero;
	}
	private JLabel getLblPrix() {
		if (lblPrix == null) {
			lblPrix = new JLabel("Prix : ");
			lblPrix.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblPrix.setBounds(277, 299, 53, 14);
		}
		return lblPrix;
	}
	private JLabel getLblMaison() {
		if (lblMaison == null) {
			lblMaison = new JLabel("Distibu\u00E9 par : ");
			lblMaison.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblMaison.setBounds(277, 319, 114, 23);
		}
		return lblMaison;
	}
	private JButton getBtnAjouter() {
		if (btnAjouter == null) {
			btnAjouter = new JButton("Ajouter");
			btnAjouter.setIcon(new ImageIcon(Album.class.getResource("/images/Boutons/Ajouter.png")));
			btnAjouter.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnAjouter.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnAjouter.setBounds(23, 23, 121, 23);
			btnAjouter.addActionListener(this);
		}
		return btnAjouter;
	}
	private JButton getBtnModifier() {
		if (btnModifier == null) {
			btnModifier = new JButton("Modifier");
			btnModifier.setIcon(new ImageIcon(Album.class.getResource("/images/Boutons/Post.png")));
			btnModifier.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnModifier.setEnabled(false);
			btnModifier.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnModifier.setBounds(23, 72, 121, 23);
			btnModifier.addActionListener(this);
		}
		return btnModifier;
	}
	private JButton getBtnSupprimer() {
		if (btnSupprimer == null) {
			btnSupprimer = new JButton("Supprimer");
			btnSupprimer.setIcon(new ImageIcon(Album.class.getResource("/images/Boutons/Supprimer.png")));
			btnSupprimer.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnSupprimer.setEnabled(false);
			btnSupprimer.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnSupprimer.setBounds(23, 118, 121, 23);
			btnSupprimer.addActionListener(this);
		}
		return btnSupprimer;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAjouter){
			AjouterModifier ajoutModif = new AjouterModifier(this, null);
			ajoutModif.setVisible(true);
		}else if(e.getSource() == btnModifier){
			AjouterModifier ajoutModif = new AjouterModifier(this, albums.getAlbum(albumTable.getSelectedRow()));
			ajoutModif.setVisible(true);
		}else if(e.getSource() == btnSupprimer){
			albums.getAlbum(albumTable.getSelectedRow()).supprimer();
			albumTable.clearSelection();
			update();
		}else if(e.getSource() == btnRecherche){
			Recherche recherche = new Recherche();
			recherche.setVisible(true);
		}else if(e.getSource() == btnRetour){
			dispose();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount() == 2){
			AjouterModifier ajoutModif = new AjouterModifier(this, albums.getAlbum(albumTable.getSelectedRow()));
			ajoutModif.setVisible(true);
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	@Override
	public void mouseExited(MouseEvent e) {
	}
	@Override
	public void mousePressed(MouseEvent e) {
	}
	@Override
	public void mouseReleased(MouseEvent e) {
	}

	private JButton getBtnRecherche() {
		if (btnRecherche == null) {
			btnRecherche = new JButton("Recherche");
			btnRecherche.setIcon(new ImageIcon(Album.class.getResource("/images/Boutons/Recherche.png")));
			btnRecherche.setCursor(new Cursor(Cursor.HAND_CURSOR));
			btnRecherche.setFont(new Font("Andalus", Font.PLAIN, 12));
			btnRecherche.addActionListener(this);
			btnRecherche.setBounds(23, 168, 121, 23);
		}
		return btnRecherche;
	}
	private JLabel getLblTitreAlbum() {
		if (lblTitreAlbum == null) {
			lblTitreAlbum = new JLabel("");
			lblTitreAlbum.setForeground(new Color(0, 0, 128));
			lblTitreAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblTitreAlbum.setBounds(323, 224, 171, 14);
		}
		return lblTitreAlbum;
	}
	private JLabel getLblGenreAlbum() {
		if (lblGenreAlbum == null) {
			lblGenreAlbum = new JLabel("");
			lblGenreAlbum.setForeground(new Color(0, 0, 128));
			lblGenreAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblGenreAlbum.setBounds(333, 249, 161, 14);
		}
		return lblGenreAlbum;
	}
	private JLabel getLblAnneeAlbum() {
		if (lblAnneeAlbum == null) {
			lblAnneeAlbum = new JLabel("");
			lblAnneeAlbum.setForeground(new Color(0, 0, 128));
			lblAnneeAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblAnneeAlbum.setBounds(404, 274, 90, 14);
		}
		return lblAnneeAlbum;
	}
	private JLabel getLblMaisonAlbum() {
		if (lblMaisonAlbum == null) {
			lblMaisonAlbum = new JLabel("");
			lblMaisonAlbum.setForeground(new Color(0, 0, 128));
			lblMaisonAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblMaisonAlbum.setBounds(380, 319, 114, 23);
		}
		return lblMaisonAlbum;
	}
	private JLabel getLblPrixAlbum() {
		if (lblPrixAlbum == null) {
			lblPrixAlbum = new JLabel("");
			lblPrixAlbum.setForeground(new Color(0, 0, 128));
			lblPrixAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblPrixAlbum.setBounds(321, 299, 173, 14);
		}
		return lblPrixAlbum;
	}
	private JLabel getLblNumeroAlbum() {
		if (lblNumeroAlbum == null) {
			lblNumeroAlbum = new JLabel("");
			lblNumeroAlbum.setForeground(new Color(0, 0, 128));
			lblNumeroAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblNumeroAlbum.setBounds(594, 224, 79, 14);
		}
		return lblNumeroAlbum;
	}
	private JLabel getLblArtiste() {
		if (lblArtiste == null) {
			lblArtiste = new JLabel("Artiste : ");
			lblArtiste.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblArtiste.setBounds(520, 249, 79, 14);
		}
		return lblArtiste;
	}
	private JLabel getLblArtisteAlbum() {
		if (lblArtisteAlbum == null) {
			lblArtisteAlbum = new JLabel("");
			lblArtisteAlbum.setForeground(new Color(0, 0, 128));
			lblArtisteAlbum.setFont(new Font("Andalus", Font.PLAIN, 18));
			lblArtisteAlbum.setBounds(594, 249, 79, 14);
		}
		return lblArtisteAlbum;
	}
}
